<?php
namespace App\Http\Controllers\Company;

use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Exceptions\NotLoginException;
use App\Models\ClientUserTicket;
use App\Models\ClientUserRoles;

use App\Models\House;
use App\Models\ServiceTeam;
use App\Models\ServiceTeamDetail;
use Illuminate\Support\Facades\DB;
/**
 * Class CommonController
 * @package App\Http\Controllers\Company
 *
 * 物业版 API 接口部分
 */
class CommonController extends Controller {
	protected $user = [];
	protected $uid;
	protected  $ship;
	// 物业版需要全局登录
	public function __construct(){
		$this->check_login();
	}
	
	public function check_login(){
		$clientUserTicket = new ClientUserTicket();
		$user = $clientUserTicket->checkLogin();
		if($user->type != 2 && $user->type != 5){
			throw new ApiException('用户角色错误，拒绝登录');
		}
		$this->user = $user;
		$this->uid = $user->id;
        if($user->type == 2) {
            $this->ship = ClientUserRoles::select(['client_user_roles.id', 'client_user_roles.name', 'client_user_roles.ship_id', 'ship.uid', 'ship.parent_id', 'ship.cmds', 'ship.type', 'ship.house_id', 'ship.village_id'])->leftJoin('client_user_relationship as ship', 'ship.id', '=', 'client_user_roles.ship_id')->where('client_user_roles.uid', '=', $user->id)->first();
        }else if( $user->type == 5){
            $service_team_detail = ServiceTeamDetail::where(['uid'=>$this->uid])->first();
            $service_team = ServiceTeam::where(['id'=>$service_team_detail->team_id])->first();
            $this->ship->name = $service_team_detail->name;
            $this->ship->cmds = '';
            $this->ship->team_id = $service_team_detail->team_id;
            $this->ship->house_id = $service_team->partition_id;
            $this->ship->village_id = House::where(['partition_id'=>$service_team->partition_id])->value('village_id');
        }
	}
}